package com.dwxt.community.service;

import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.util.PartyUtil;
import com.dwxt.community.dao.UserActionRecordMapper;
import com.dwxt.community.entity.IntegralRule;
import com.dwxt.community.entity.User;
import com.dwxt.community.entity.UserActionRecord;
import com.dwxt.community.entity.enumerate.ActionInfoEnum;
import com.dwxt.community.entity.vo.UserActionRecordVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

@Service
public class UserActionRecordService {

    @Autowired
    private UserActionRecordMapper userActionRecordMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private IntegralService integralService;

    private Logger logger = LoggerFactory.getLogger(UserActionRecordService.class);

    /**
     * 分享
     *
     * @param id
     * @return
     */
    public BaseResult share(String modelInfo, String id) {
        String currentUserId = PartyUtil.getCurrentUserId();
        addActionRecord(id, StringUtils.isNotBlank(currentUserId)?currentUserId:"游客", modelInfo, ActionInfoEnum.share.getCode());
        if (StringUtils.isNotBlank(currentUserId)){
            User user = userService.selectUserByPrimaryKey(currentUserId);
            if (ObjectUtils.isNotEmpty(user)) {
                IntegralRule integralRule = integralService.selectRuleByCategory(modelInfo+"分享");
                if (ObjectUtils.isEmpty(integralRule)) {
                    return BaseResult.build(StatusCode.PARAMETER_WRONG);
                }
                int shareCount = integralService.countTodayByCategoryAndUserId(integralRule.getCategory(), currentUserId);
                if (shareCount >= integralRule.getCountLimit() ){
                    return BaseResult.build(StatusCode.SHARE_SAT);
                }
                //添加积分
                userService.updateIntegral(integralRule.getIntegral(), null, integralRule.getCategory(), currentUserId);
            }
        }
        return BaseResult.ok();
    }

    public BaseResult hit(String modelInfo, String id) {
        String currentUserId = PartyUtil.getCurrentUserId();
        addActionRecord(id, currentUserId, modelInfo, ActionInfoEnum.hit.getCode());
        return BaseResult.ok();
    }

    /**
     * 添加行为记录
     */
    public void addActionRecord(String modelId, String userId, String modelInfo, Integer actionType) {
        UserActionRecord userActionRecord = new UserActionRecord();
        userActionRecord.setModelId(modelId);
        userActionRecord.setUserId(StringUtils.isBlank(userId) ? "游客" : userId);
        userActionRecord.setModelInfo(modelInfo);
        userActionRecord.setActionType(actionType);
        Calendar calendar = Calendar.getInstance();
        userActionRecord.setYear(calendar.get(Calendar.YEAR));
        userActionRecord.setMonth(calendar.get(Calendar.MONTH) + 1);
        userActionRecord.setDay(calendar.get(Calendar.DAY_OF_MONTH));
        userActionRecord.setCreateTime(calendar.getTime());
        if (userActionRecordMapper.insert(userActionRecord) < 1) {
            logger.warn("用户行为记录插入失败 => 重要字段为空！");
        }
    }


    public List<UserActionRecordVo> countByCreateTime(String date) {
        return userActionRecordMapper.countByCreateTime(date);
    }

    public List<UserActionRecordVo> countStrByCreateTime(String date) {
        return userActionRecordMapper.countStrByCreateTime(date);
    }
}
